package com.krain.meijusky.activity;

import android.content.Intent;

import com.krain.corelibrary.widget.RefreshRecyclerView;
import com.krain.corelibrary.widget.loadingView.LoadingView;
import com.krain.meijusky.R;
import com.krain.meijusky.adapter.SearchResultAdapter;
import com.krain.meijusky.entity.ResourceEntity;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.Extra;
import org.androidannotations.annotations.ViewById;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Class description
 *
 * @author yamin
 * @date 2015/11/23 下午 01:11
 */
@EActivity(R.layout.activity_searchresult)
public class CategoryListActivity extends SearchResultActivity {
    private String rootUrl = "";

    private List<ResourceEntity> mEntityList;
    @ViewById(R.id.loadView)
    LoadingView mLoadingView;

    @Extra("name")
     String mTitle;

    @Extra("key")
     String categoryWord = "";

    @Override
    protected String getToolbarTitle() {
        return mTitle;
    }

    @Override
    void afterViews() {
        rootUrl = getString(R.string.category_list_rooturl,categoryWord);
        mRefreshRecyclerView.showRefreshing(false);
        mRefreshRecyclerView.setIsLoadMore(false);
        getSearchResult(mEntityList,mRefreshRecyclerView,mLoadingView);
    }

    @Override
    protected void getSearchResult(List<ResourceEntity> mEntityList, RefreshRecyclerView mRefreshRecyclerView, LoadingView loadingView) {
        try {
            Document document = Jsoup.connect(rootUrl).get();
            Elements listElements = document.select("div[class=bor_img3_right]");
            Elements listAttribute = document.select("ul[class=list_20]");
            mEntityList = new ArrayList<>();
            getListEntity(listElements, listAttribute, mEntityList,mRefreshRecyclerView,loadingView);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void jumpTothisActivity(BaseActivity activity,String name,String key){
        Intent intent = new Intent(activity,CategoryListActivity_.class);
        intent.putExtra("name",name);
        intent.putExtra("key",key);
        activity.startActivity(intent);
    }

}
